<template>
  <div class="app-container">
    <div class="box1">
      <button
        v-for="(item, index) in mallList"
        :key="'malls-' + index"
        class="custom-button"
        :class="{ active: selectedMalls === item }"
        @click="selectMalls(item)"
      >
        {{ item.name }}
      </button>
    </div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="批次编号" prop="batchNumber">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.batchNumber"-->
      <!--          placeholder="请输入批次编号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="交接编号" prop="handoverNumber">
        <el-input
          v-model="queryParams.handoverNumber"
          placeholder="请输入交接编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="交接时间" prop="handoverTime">
        <el-date-picker clearable
                        v-model="queryParams.handoverTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择交接时间">
        </el-date-picker>
      </el-form-item>
      <!--      <el-form-item label="交接人" prop="handoverBy">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.handoverBy"-->
      <!--          placeholder="请输入交接人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="交接人名称" prop="handoverByName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.handoverByName"-->
      <!--          placeholder="请输入交接人名称"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="接收人" prop="receiveByName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.receiveByName"-->
      <!--          placeholder="请输入接收人"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="合同编号" prop="contractNumber">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.contractNumber"-->
      <!--          placeholder="请输入合同编号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="运输编号" prop="deliveryNumber">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.deliveryNumber"-->
      <!--          placeholder="请输入运输编号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['erp:handover:add']"
        >新增</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['erp:handover:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['erp:handover:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['erp:handover:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="handoverList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="批次编号" align="center" prop="batchNumber" />
      <el-table-column label="批次编号" align="center" prop="handoverNumber" />
      <el-table-column label="交接时间" align="center" prop="handoverTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handoverTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="交接单文件" align="center" prop="file">
        <template slot-scope="scope">
          <el-link :href="scope.row.fileUrl" target="_blank" type="primary">{{ '点击打开交接单' }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="上传时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-edit"-->
          <!--            @click="handleUpdate(scope.row)"-->
          <!--            v-hasPermi="['erp:handover:edit']"-->
          <!--          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['erp:handover:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改交接单信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="交接单号" prop="handoverNumber">
          <el-input v-model="form.handoverNumber" placeholder="请输入交接单号" />
        </el-form-item>
        <el-form-item label="交接单文件" prop="file">
          <el-upload
            class="upload-demo"
            :action="uploadUrl"
            :headers="headers"
            :on-success="handleSuccess"
            :on-error="handleError"
            :before-upload="beforeUpload"
            :limit="1"
            :file-list="fileList"
            list-type="picture"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listHandover, getHandover, delHandover, addHandover, updateHandover,uploadFile } from "@/api/erp/handover";
import {getMall} from "@/api/erp/good"
import request from "../../../utils/request";
import {getToken} from "@/utils/auth";
export default {
  name: "Handover",
  data() {
    return {
      // 遮罩层
      loading: true,
      uploadUrl:'/dev-api/erp/order/uploadFile',
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //店铺数据
      mallList: [],
      headers: {
        Authorization: "Bearer " + getToken(),
      },
      fileList: [],
      // 总条数
      total: 0,
      // 交接单信息表格数据
      handoverList: [],
      selectedMalls: null,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        mallId: null,
        courierName: null,
        courierMobile: null,
        batchNumber: null,
        handoverNumber: null,
        handoverTime: null,
        handoverBy: null,
        handoverByName: null,
        receiveByName: null,
        contractNumber: null,
        deliveryNumber: null,
        orderNumber1: null,
        orderNumber2: null,
        file: null,
        fileUrl: null,
        alterFiles: null,
        signFile: null,
        signFileUrl: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "用户ID不能为空", trigger: "blur" }
        ],
        mallId: [
          { required: true, message: "店铺ID不能为空", trigger: "blur" }
        ],
        batchNumber: [
          { required: true, message: "批次编号不能为空", trigger: "blur" }
        ],
        handoverNumber: [
          { required: true, message: "交接编号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态 1创建 2完成不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询交接单信息列表 */
    getList() {
      this.loading = true;
      listHandover(this.queryParams).then(response => {
        this.handoverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        mallHandoverId: null,
        userId: null,
        mallId: null,
        courierName: null,
        courierMobile: null,
        batchNumber: null,
        handoverNumber: null,
        handoverTime: null,
        handoverBy: null,
        handoverByName: null,
        receiveByName: null,
        contractNumber: null,
        deliveryNumber: null,
        orderNumber1: null,
        orderNumber2: null,
        file: null,
        fileUrl: null,
        alterFiles: null,
        signFile: null,
        signFileUrl: null,
        status: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      const mallId = this.selectedMalls.mallId
      this.queryParams.mallId = mallId;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.mallHandoverId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加交接单信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const mallHandoverId = row.mallHandoverId || this.ids
      getHandover(mallHandoverId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改交接单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.handoverNumber == null) {
        this.$message.warning("请填写交接单号")
        return
      }
      console.log(this.form)
      this.form.mallId = this.selectedMalls.mallId
      this.form.fileUrl = this.fileUrl
      addHandover(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const mallHandoverIds = row.mallHandoverId || this.ids;
      this.$modal.confirm('是否确认删除交接单信息编号为"' + mallHandoverIds + '"的数据项？').then(function () {
        return delHandover(mallHandoverIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('erp/handover/export', {
        ...this.queryParams
      }, `handover_${new Date().getTime()}.xlsx`)
    },
    //获取店铺信息
    getMall() {
      this.loading = true;
      return getMall().then(response => {
        this.mallList = response.data
        this.loading = false;
        return response;
      })
    },
    selectMalls(item) {
      this.selectedMalls = item;
      this.handleQuery()
    },
    handleSuccess(response, file, fileList) {
      this.$message.success("文件上传成功");
      this.fileUrl = response
      this.fileList = fileList;
    },
    handleError(err, file, fileList) {
      this.$message.error("文件上传失败");
      this.fileList = fileList;
    },
    beforeUpload(file) {
      const isPDF = file.type === "application/pdf";
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 10;
      if ((!isJPG && !isPNG && !isPDF) || (!isLt2M)) {
        let message = "上传文件不符合要求";
        if (!isLt2M) {
          message = "上传文件大小不能超过10MB！";
        } else if (!isPDF && !isJPG && !isPNG) {
          message = "请上传PDF、JPG或PNG格式的文件！";
        }
        this.$message.error(message);
        return false;
      }
      return true;
    },
  },
  mounted() {
    this.getMall().then(() => {
      this.selectedMalls = this.mallList[0];
      this.handleQuery()
    })
  }
};
</script>
<style>
.button-container {
  display: flex; /* 使用 Flexbox 来排列按钮 */
}

.custom-button {
  background-color: transparent;
  color: rgba(0, 0, 0, 0.6); /* 默认文字颜色为黑色 */
  border: 0.2px solid rgba(0, 0, 0, 0.3);
  padding: 3px 3px;
  font-size: 16px;
  transition: background-color 0.3s, color 0.3s, border-color 0.3s;
  min-width: 80px; /* 设置按钮的最小宽度 */
  min-height: 40px; /* 设置按钮的最小高度 */
  display: inline-flex; /* 使按钮大小根据内容调整 */
  align-items: center; /* 垂直居中对齐文字 */
  justify-content: center; /* 水平居中对齐文字 */

}

.custom-button:hover {
  background-color: white; /* 悬停时背景颜色保持白色 */
  color: #1890FF; /* 悬停时文字颜色变为蓝色 */
}

.custom-button:active {
  background-color: white;
  color: black;
}

.custom-button:focus {
  background-color: #1890FF; /* 点击或选中时背景颜色变为蓝色 */
  color: white; /* 点击或选中时文字颜色变为白色 */
  border-color: dodgerblue; /* 点击或选中时边框颜色变为蓝色 */
}

.custom-button:not(:last-of-type) {
  border-right: none; /* 去掉按钮之间的右边框 */
}

.active {
  background-color: dodgerblue;
  color: white;
}

.box1 {
  margin-bottom: 20px;
}
</style>

